package middleware

import (
	"git.mycaigou.com/gfyx/common/authorize"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/errcode"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/utils"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/utils/jwt"
	"git.myscrm.cn/golang/stark/v4"
	"github.com/gin-gonic/gin"
)

func AuthorizeOmp(excludes []string) gin.HandlerFunc {
	return func(ctx *gin.Context) {
		// 1. 排除鉴权的路由
		if utils.InArray(excludes, ctx.Request.URL.Path) {
			ctx.Next()
			return
		}

		// 2. 调用 jwt 库解析用户传参的 Token
		_, err := jwt.NewJwtOmp().ParserJwtToken(ctx)

		token, _ := ctx.Get("Authorization")
		if err != nil {
			stark.Logger.Errorf(ctx, "AuthorizeOmp-err: %s, Authorization: %s", err.Error(), token)
			response(ctx, errcode.COMMON_TOKEN_ERROR, "令牌解析错误")
			return
		}

		ctx.Set(authorize.TENANT_CODE_KEY, "ycg")
		ctx.Next()
	}
}
